layui.use('table', function () {
    var table = layui.table;
    //监听表格复选框选择
    table.on('checkbox(demo)', function (obj) {
        // console.log(obj)
    });



    //方法级渲染，负责渲染表格数据
    table.render({
        elem: '#table_carousel_lists'
        , url: '/carouselCount'
        , cols: [[
            {checkbox: true, fixed: true}
            , {field: 'adminId', title: '商家ID', width: 100, sort: true, fixed: true}
            , {field: 'name', title: '商铺名', width: 160}
            , {field: 'address', title: '商铺地址', width: 150}
            , {
                field: 'type',
                title: '轮播类型',
                width: 100,
                templet: "<div>{{(d.type == 1) ? '商品':(d.type == 2)?'分类':(d.type == 3)?'暂无此分类':'待审核'}}</div>"
            }
            , {
                field: 'createTime',
                field: 'createTime',
                title: '创建建时间',
                with: 160,
                templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
            }
            , {
                field: 'startTime',
                title: '开始时间',
                with: 160,
                templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
            }
            , {
                field: 'expiredTime',
                title: '到期时间',
                with: 160,
                templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
            }
            // , {field: 'userEmail', title: '商家电话', sort: true, width: 250}
            , {
                field: 'isShow',
                title: '状态',
                width: 100,
                sort: true,
                templet: "<div>{{(d.isShow == 1) ? '展示':(d.isShow == 0)?'不展示':(d.isShow == 2)?'待展示':(d.isShow ==3)?'待审核':'审核不通过'}}</div>"
            }
            , {field: 'right', title: '操作', toolbar: '#carouselDemo', width: 150}
        ]]
        , id: 'testReload'
        , page: true
        , limit: 10
        , height: 460
    });
    //监听工具条
    table.on('tool(carousel)', function (obj) {
        var data = obj.data;
        if (obj.event === 'forbidden') {

            $.ajax({
                    contentType: "application/json",
                    url: "/carouselForbidden",
                    method: "post",
                    data: JSON.stringify({"id": data.id}),
                    dataType: "text",
                    success: function (data) {
                        if (data == "禁用成功") {
                            alert("禁用成功")
                            $(".layui-laypage-btn")[0].click();
                        } else {
                            alert("禁用失败")
                        }
                    },
                    error: function () {
                        alert("网络繁忙，正在尝试重新链接")
                    },
                }
            )
        } else if (obj.event === 'start') {
            // alert(data.id)
            $.ajax({
                    contentType: "application/json",
                    url: "/carouselStart",
                    method: "post",
                    data: JSON.stringify({"id": data.id}),
                    dataType: "text",
                    success: function (data) {
                        if (data == "展示成功") {
                            alert("展示成功")
                            $(".layui-laypage-btn")[0].click();
                        } else {
                            alert("展示失败")
                        }
                    },
                    error: function () {
                        alert("网络繁忙，正在尝试重新链接")
                    },
                }
            )
        } else if (obj.event === 'pass') {
            var url = data.imageUrl;
            var shopName = data.name;
            var shopAddress = data.address;
            var startTime = formatDate(data.startTime);
            var expiredTime = formatDate(data.expiredTime);
            var id =data.id;
            // console.log(data)
            layer.confirm(
                '<div class="layui-row">' +
                '<div class="layui-col-xs6">' +
                ' <div class="grid-demo grid-demo-bg1">' +
                '<table style="margin-left: 90px">' +
                '<tr>' +
                    '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                    '<td style="font-size: 25px">&nbsp '+'</td>' +
                '</tr>' +
                '<tr>' +
                '<td style="font-size: 25px">商&nbsp&nbsp家&nbsp&nbsp名： ' + '</td>' +
                '<td style="font-size: 25px">' + shopName + '</td>' +
                '</tr>' +

                '<tr>' +
                '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                '<td style="font-size: 25px">&nbsp ' + '</td>' +
                '</tr>' +
                '<tr>' +
                '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                '<td style="font-size: 25px">&nbsp ' + '</td>' +
                '</tr>' +
                '<tr>' +
                '<td style="font-size: 25px">商&nbsp家地&nbsp址： ' + '</td>' +
                '<td style="font-size: 25px">' + shopAddress + '</td>' +
                '</tr>' +
                '<tr>' +
                '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                '<td style="font-size: 25px">&nbsp ' + '</td>' +
                '</tr>' +

                '<td style="font-size: 25px">开&nbsp始时&nbsp间： ' + '</td>' +
                '<td style="font-size: 25px">' + startTime + '</td>' +
                '</tr>' +
                '<tr>' +
                '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                '<td style="font-size: 25px">&nbsp ' + '</td>' +
                '</tr>' +

                '<tr>' +
                '<td style="font-size: 25px">结&nbsp束时&nbsp间： ' + '</td>' +
                '<td style="font-size: 25px">' + expiredTime + '</td>' +
                '<td >' + '</td>' +

                '</tr>' +
                '<tr>' +
                '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                '<td style="font-size: 25px">&nbsp ' + '</td>' +
                '</tr>' +
                '<tr>' +
                '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                '<td style="font-size: 25px">&nbsp ' + '</td>' +
                '</tr>' +
                '<tr>' +
                '<td style="font-size: 25px">&nbsp&nbsp&nbsp&nbsp ' + '</td>' +
                '<td style="font-size: 25px">&nbsp ' + '</td>' +
                '</tr>' +
                '<tr>' +
                '<td>' + '<input type="button" class="layui-btn"  style="width: 80px" title="'+id+'"  onclick="pass1(this)" value="通过">' + '</td>' +
                '<td>' + '<input  type="button" class="layui-btn"  style="width: 80px" title="'+id+'"  onclick="noPass1(this)" value="不通过">' + '</td>' +
                '</tr>' +
                '</table>' +
                '</div>' +
                '</div>' +
                '<div class="layui-col-xs6">' +
                ' <div class="grid-demo"><img width="600px" height="490px" src=' + url + '>'
                + '<div></div>' +
                '</div>' +
                '</div>' +
                '</div>'
                , {
                    title: '提示',
                    btn: ['取消'],
                    area: ['1200px', '500px']
                }, function (index) {
                    // alert(JSON.stringify(data));
                    // var newPwd = $("#newPwd").val();
                    layer.closeAll();


                });

        } else if (obj.event === 'ouLoad'){//上传图片
            $('#id').val(data.id);//放入隐藏域
            layer.open({
                type: 1
                ,area: ['800px', '400px']
                ,content: $('#DIV_ImgInsert')
                ,btn: '提交'
                ,btnAlign: 'c' //按钮居中
                ,shade: 0 //不显示遮罩
                ,yes: function(){
                    alert("成功");
                }
            });
        }
    });

    //此处为重载方法，来自layui
    var $ = layui.$, active = {
        reload: function () {
            var adminName = $('#adminName');
            var carouselStatus = $('#carouselStatus').val();
            //执行重载
            table.reload('testReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {
                    name: adminName.val(),
                    isShow:carouselStatus,
                }
            }, 'data');
        }
    };
    //此处方法来自layui，执行重载方法的部分
    $('#layerDemo .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});

function formatDate(date) {
    var date = new Date(date);
    var YY = date.getFullYear() + '-';
    var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    return YY + MM + DD;
}

function pass1(node) {
    // alert("进来了")
    var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
   var id= $(node).prop("title")
    $.ajax({
            // contentType: "application/json",
            url: "/carouselPass",
            method: "post",
            data: "id="+id,
            dataType: "text",
            success: function (data) {
                if (data == "启用成功") {
                    alert("启用成功")
                    $(".layui-laypage-btn")[0].click();
                    layer.closeAll();
                } else {
                    alert("启用失败")
                }

            },
            error: function () {
                alert("网络繁忙，正在尝试重新链接")
            },
        })

}

function noPass1(node) {
    // alert("进来了")
    var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
    var aId= $(node).prop("title")
    $.ajax({
        // contentType: "application/json",
        url: "/carouselNoPass",
        method: "post",
        data: "aId="+aId,
        dataType: "text",
        success: function (data) {
            if (data == "更新成功") {
                alert("更新成功")
                $(".layui-laypage-btn")[0].click();
                layer.closeAll();
            } else {
                alert("更新失败")
            }

        },
        error: function () {
            alert("网络繁忙，正在尝试重新链接")
        },
    })

}


layui.use('laydate', function(){
    var laydate = layui.laydate;


    //日期时间范围
    laydate.render({
        elem: '#test10'
        ,type: 'datetime'
        ,range: true,
        format: 'yyyy-MM-dd'
    });
});

layui.use(['upload','form'],function(){
    var $ = layui.jquery
        ,upload = layui.upload
        ,form = layui.form;
    //绑定原始文件域
    upload.render({
        elem: '#test20'
        // ,url: 'https://httpbin.org/post' //改成您自己的上传接口
        ,done: function(res){
            layer.msg('上传成功');
            console.log(res)
        }
    });
    form.on('select(choose)',function (data) {
        // console.log(data.value)
        $("#sonId").html("<option value=\"\">请选择</option>");
        $.ajax({
            // contentType: "application/json",
            url: "/getChoose",
            method: "post",
            data: "choose="+data.value,
            dataType: "json",
            success: function (data) {
                // console.log(data)
                // console.log(data[1].name)
                if (data[1].name != null){
                    $("#sonId").attr("name","catId");
                }
                if (data[1].itemName != null){
                    $("#sonId").attr("name","itemId");
                }
                var str= "";
                for(var key in data){
                    if (data[key].name != null){
                        str += "<option value="+data[key].id+">"+data[key].name+"</option>";
                    }
                    if (data[key].itemName != null){
                        str += "<option value="+data[key].id+">"+data[key].itemName+"</option>";
                    }
                }
                $("#sonId").append(str);
                form.render('select');

            },
            error: function () {
                alert("网络繁忙，正在尝试重新链接")
            },
        })
    })
});



function createNewAdvertising() {
    layui.use(['layer'], function () {
        let layer = layui.layer, $ = layui.$;
        layer.open({
            type: 1,
            anim:4,
            area:['700px','550px'],
            title: '新增分类',
            shadeClose: false,
            content: $('#creatNewAd')
        });
    });
}




//创建广告
function createAdvertise() {
    // alert("进来了")
    // console.log($("#choose option"))
    var type = $("#choose").val();
    var imageUrl = $("input[name='catImage']").val();
    var itemId = $('#sonId').val();
    var startTime ;
    var expiredTime ;
    var time = $("#test10").val();
    // alert(time)
    if (time!=""&&time!=null) {
        var data=time.split(" ",4)
        // alert(data[0])
        // alert(data[3])
        startTime= data[0];
        expiredTime=  data[3] ;
    }
    // alert(startTime)
    // alert(expiredTime)
    var data = {
        "type": type,
        "imageUrl": imageUrl,
        "itemId": itemId,
        "startTime": startTime,
        "expiredTime": expiredTime
    }
    $.ajax({
        contentType: "application/json",
        url: "/findOption",
        method: "post",
        data: JSON.stringify(data),
        dataType: "text",
        success: function (data) {
            if (data=="上传成功"){
                alert("上传成功")
                layer.closeAll();

            }else{
                alert("上传失败")
            }
        },
        error: function () {
            alert("网络繁忙，正在尝试重新链接")
        },
    })

}


